/*
 * Stellarium-Collada Plug-in
 * 
 * Copyright (C) 2010 Simon Parzer, Gustav Oberwandling, Peter Neubauer
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "Collada.hpp"

#include "StelApp.hpp"
#include "StelGui.hpp"
#include "StelGuiItems.hpp"
#include "StelLocaleMgr.hpp"
#include "StelModuleMgr.hpp"
#include "StelCore.hpp"
#include "StelPainter.hpp"

#include <QAction>
#include <QString>
#include <QDebug>
#include <stdexcept>

////////////////////////////////////////////////////////////////////////////////
// 
StelModule* ColladaStelPluginInterface::getStelModule() const
{
	return new Collada();
}

StelPluginInfo ColladaStelPluginInterface::getPluginInfo() const
{
	// Allow to load the resources when used as a static plugin
	Q_INIT_RESOURCE(Collada);
	
	StelPluginInfo info;
	info.id = "Collada";
	info.displayedName = "Collada";
	info.authors = "Simon Parzer, Gustav Oberwandling, Peter Neubauer";
	info.contact = "/dev/null";
	info.description = "Collada loader and renderer.";
	return info;
}

Q_EXPORT_PLUGIN2(Collada, ColladaStelPluginInterface)


////////////////////////////////////////////////////////////////////////////////
// Constructor and destructor
Collada::Collada()
: obj()
{
	setObjectName("Collada");
}

Collada::~Collada()
{

}


////////////////////////////////////////////////////////////////////////////////
// Methods inherited from the StelModule class
// init(), update(), draw(), setStelStyle(), getCallOrder()
void Collada::init()
{
	qDebug() << "Collada::init()";
	try
	{
                obj.load("test.obj");
                obj.prepareStelModels();
	}
	catch (std::runtime_error &e)
	{
		qWarning() << "Collada::init() error: " << e.what();
		return;
	}
}

void Collada::deinit()
{
	qDebug() << "Collada::deinit()";
}

void Collada::update(double deltaTime)
{
}

void Collada::draw(StelCore* core)
{
    obj.draw(core);
}

//! Key events. stellarium-collada uses WASD for movement.
void Collada::handleKeys (class QKeyEvent *e)
{
	char c = '\0';
	
	switch (e->key())
	{
		case Qt::Key_W:
			c = 'W';
			break;
		case Qt::Key_A:
			c = 'A';
			break;
		case Qt::Key_S:
			c = 'S';
			break;
		case Qt::Key_D:
			c = 'D';
			break;
		default:
			return;
	}
	
	switch (e->type())
	{
		case QEvent::KeyPress:
			qDebug() << QString("Collada: movement key '%1' pressed.").arg(c);
			break;
		case QEvent::KeyRelease:
			qDebug() << QString("Collada: movement key '%1' released.").arg(c);
			break;
		default:
			break;
	}
}
	
double Collada::getCallOrder(StelModuleActionName actionName) const
{
	if (actionName == StelModule::ActionHandleKeys)
	{
		// we desperately need priority on WASD keys; take it by force
		return -1; // TODO: this is not enough / doesn't work
	}
	else if (actionName == StelModule::ActionDraw)
	{
		return 500; // after landscape etc, but before stel. GUI
	}
	return 0.;
}

bool Collada::configureGui(bool show)
{
	return true;
}

